const render = require('koa-swig');
const co = require('co');
const path = require('path');
const config = require('../config/config'); 

const initRenders = {
    async init(app, ctx){
        // var filters = {
        //     formatVersion: function (version) {
        //         return '@V' + version;
        //     }
        // };
        // var renderData = {
        //     title: 'Praise Button',
        //     version: '4.0'
        // };

        app.context.render = co.wrap(render({  
            root: config.get('viewsDir'),
            writeBody: false,
            filters: config.get('title-filter')
        }));
        return await ctx.render('index', config.get('main-renderData'));
    },
    async star(app, ctx){
        if(ctx.request.header['x-pjax']){
            return '<x-star></x-star>';
        }
        else {
            app.context.render = co.wrap(render({  
                root: config.get('viewsDir'),
                writeBody: false,
                filters: config.get('title-filter')
            }));
            return await ctx.render('star', config.get('main-renderData'));
        }
        
    },
    async praise(app, ctx){
        if (ctx.request.header['x-pjax']) {
            return '<x-praise></x-praise>';
        }
        else {
            app.context.render = co.wrap(render({  
                root: config.get('viewsDir'),
                writeBody: false,
                filters: config.get('title-filter')
            }));
            return await ctx.render('index', config.get('main-renderData'));
        }
    },
    async ad(app, ctx){
        return '<div style="height:150px;background:yellowgreen">广告位</div>';
    }
}

module.exports = initRenders;